using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

namespace BizElements.BusinessLayer
{
	/// <summary>
	/// Text source that uses resource (RESX) files.
	/// </summary>
	public class TextResource : ITextSource
	{
		#region Fields.

		ResourceManager resourceManager;
		CultureInfo currentCulture;

		#endregion

		#region Constructors.

        /// <summary>
        /// Initializes a new instance of <b>TextResource</b> class.
        /// </summary>
        /// <param name="resourceManager">Resource from which the strings are retrieved.</param>
		public TextResource(ResourceManager resourceManager)
		{
			this.resourceManager = resourceManager;
		}

        /// <summary>
        /// Initializes a new instance of <b>TextResource</b> class.
        /// </summary>
        /// <param name="baseName"><see cref="System.Resources.ResourceManager.BaseName"/> 
        /// of the resource from which the strings are retrieved.</param>
		public TextResource(string baseName)
		{
			this.BaseName = baseName;
		}

		#endregion

        #region Properties.

        /// <summary>
        /// Gets or sets the resource from which the strings are retrieved.
        /// </summary>
		public ResourceManager ResourceManager
		{
			get {return this.resourceManager;}
			set {this.resourceManager = value;}
		}

        /// <summary>
        /// Gets or sets the <see cref="System.Resources.ResourceManager.BaseName"/> of the resource from which the strings are retrieved.
        /// </summary>
		public string BaseName
		{
			get {return this.resourceManager.BaseName;}
			set {this.resourceManager = new ResourceManager(value, Assembly.GetCallingAssembly());}
        }

        #endregion

        #region ITextSource.

        /// <summary>
        /// Gets or sets the current culture.
        /// </summary>
		public CultureInfo CurrentCulture
		{
			get	{return this.currentCulture;}
			set	{this.currentCulture = value;}
		}

        /// <summary>
        /// Gets string with the specified key. Text is in current culture language.
        /// </summary>
        /// <param name="key">Unique key in a text source.</param>
        /// <returns>String that matches the given key. Null if it doesn't exist.</returns>
		public string GetString(string key)
		{
			return GetString(key, this.currentCulture);
		}

        /// <summary>
        /// Gets string with the given key written for the specified culture/language.
        /// </summary>
        /// <param name="key">Unique key in a text source.</param>
        /// <param name="culture">Specifies culture and language of the requested string.</param>
        /// <returns>String that matches the given key. Null if it doesn't exist.</returns>
		public string GetString(string key, CultureInfo culture)
		{
			string message = null;			
			if (culture == null)
			{
				try		{message = this.resourceManager.GetString(key);}
				catch	{}
			}
			else
			{
				try		{message = this.resourceManager.GetString(key, culture);}
				catch	{}
			}

			return message;
		}

        /// <summary>
        /// Resets CurrentCulture property.
        /// </summary>
		public void UseDefaultCulture()
		{
			this.currentCulture = null;
		}

		#endregion		
	}
}